package org.kabeja.tools;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author shutao
 * @since 2022/4/25 10:37
 */
public class CodePageParser {
    public static final String CODEPAGE_CODE = "$DWGCODEPAGE";
    public static final String GROUPCODE = "3";
    private static final String[] PREFIX = new String[]{"ansi_", "dos"};
    private static final String     JAVA_PREFIX = "Cp";

    public CodePageParser() {
    }

    public String parseEncoding(BufferedReader var1) {
        String var2 = "";

        try {
            String var3 = null;
            boolean var6 = true;
            boolean var7 = false;
            boolean var8 = true;
            String var9 = null;

            label58:
            do {
                while(true) {
                    while((var3 = var1.readLine()) != null && var6) {
                        var3 = var3.trim();
                        if (!var8) {
                            var8 = true;
                            if ("ENDSEC".equals(var3)) {
                                return var2;
                            }

                            if (!CODEPAGE_CODE.equals(var3)) {
                                if (var7 && GROUPCODE.equals(var9)) {
                                    return this.translateCodePage(var3);
                                }
                                continue label58;
                            }

                            var7 = true;
                        } else {
                            var9 = var3;
                            var8 = false;
                        }
                    }

                    return var2;
                }
            } while(!"CLASSES".equals(var3) && !"BLOCKS".equals(var3) && !"ENTITIES".equals(var3));

            return var2;
        } catch (IOException var10) {
            var10.printStackTrace();
            return var2;
        }
    }

    public String translateCodePage(String var1) {
        String var2 = var1.toLowerCase();

        for (String s : PREFIX) {
            if (var2.startsWith(s)) {
                return JAVA_PREFIX + var1.substring(s.length());
            }
        }

        return var1;
    }
}
